/**
 * 扬尘配置项目检测点配置dustProCheckPoint.js
 * 版本号： v1.0.0
 */
$(function () {
    //查询所属项目绑定
    bindCombo_project("prId_");

    //查询配置编号绑定
    bindCombo_deviceId("deviceId_");

    //项目选择改变事件
    bindCombo_projectChange("deviceId_");
    /**
     *扬尘配置项目检测点配置列表
     */
    var lastChecked = null;
    $("#dustProCheckPoint_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        striped: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/dustProCheckPoint/dustProCheckPointList.do',
        idField: 'dpcpId',
        frozenColumns: [[
            {field: 'dpcpId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'dpcpName', title: '检测点名称', width: 180, align: 'left',fixed:true},
                {field: 'project', title: '所属项目', width: 180, align: 'left',fixed:true,
                    formatter:function(value){
                        if(value){
                            return value.prName
                        }
                    }
                },
                {field: 'deviceId', title: '配置编号', width: 150, align: 'left',fixed:true},
                {field: 'onLineStatus', title: '在线状态', width: 70, align: 'center',fixed:true,
                    formatter:function(val){
                        if (val == 1) return "<span style='color: green; font-weight: bold'>在线</span>";
                        if (val == 2) return "<span style='color: red; font-weight: bold'>离线</span>";
                    }
                },
                {field: 'createDate', title: '创建时间', width: 140, align: 'left'}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#dustProCheckPoint_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#dustProCheckPoint_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#dustProCheckPoint_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#dustProCheckPoint_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            dustProCheckPoint_doSearch();
                        }
                    });
                }
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            dustPro_add();
                        }
                    });
                }
                //修改按钮
                if("editBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            var checked = $("#dustProCheckPoint_dataList").datagrid("getChecked");
                            if (!checked || checked.length == 0) {
                                $.messager.show({ title: '操作提示', msg: "请选择一条数据后再进行修改操作", timeout: 3000, showType: "slide" });
                                return;
                            };
                            var row = $("#dustProCheckPoint_dataList").datagrid("getSelected");
                            var dpcpId = row.dpcpId;
                            window.location.href = CONTEXT_PATH + "/dustProCheckPoint/toDustProCheckPoint.do?id="+dpcpId;
                        }
                    });
                }
                //删除按钮
                if("delBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            dustPro_del();
                        }
                    });
                }
                //详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                           dustProCheckPoint_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 添加扬尘配置
 */
function dustPro_add(){
    window.location.href = CONTEXT_PATH + "/dustProCheckPoint/toDustProCheckPoint.do";
}



/**
 * 删除扬尘配置
 */
function dustPro_del() {
    var checked = $("#dustProCheckPoint_dataList").treegrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择项目检测点配置", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#dustProCheckPoint_dataList").datagrid("getSelected");
    $.messager.confirm('删除提示', '您确定要删除 ' + row.dpcpName + ' 吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/dustProCheckPoint/delDustProCheckPoint.do?id=" + row.dpcpId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#dustProCheckPoint_dataList").datagrid("reload", {
                        prId: $("#prId_").combobox("getValue"),
                        dpcpName: $("#dpcpName_").textbox("getValue"),
                        deviceId: $("#deviceId_").textbox("getValue")
                    });
                }
            })
        }
    });
}


/**
 * 执行查询
 */
function dustProCheckPoint_doSearch() {
    //刷新dg页码
    refreshPageNumber("dustProCheckPoint_dataList");

    $("#dustProCheckPoint_dataList").datagrid("reload", {
        prId: $("#prId_").combobox("getValue"),
        dpcpName: $("#dpcpName_").textbox("getValue"),
        deviceId: $("#deviceId_").textbox("getValue")
    });
    $("#dustProCheckPoint_dataList").datagrid("clearSelections");
    $('#dustProCheckPointQuery_dialog').dialog('close');
}




/**
 * 查看扬尘
 */
function dustProCheckPoint_view(iconCls){
    var row = $("#dustProCheckPoint_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的扬尘详情", timeout: 3000, showType: "slide" });
        return;
    }
    var row = $("#dustProCheckPoint_dataList").datagrid("getSelected");
    //IFRAM打开窗口
    layoutSub(ycxq.id, ycxq.url + row.dpcpId, iconCls, ycxq.closable, ycxq.title);
}

/**
 * 所属项目bind事件
 * @param projectControl
 */
function bindCombo_project(projectControl) {
    $.ajax({
        url: CONTEXT_PATH + "/project/getProjectListForCombo.do",
        type: "get",
        success: function (data) {
            var obj = JSON.parse(data);
            var projectCombo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < obj.length; i++) {
                projectCombo.push({"text": obj[i].prName, "value": obj[i].prId});
            }
            $("#" + projectControl).combobox("loadData", projectCombo);
        }
    })
}

/**
 * 配置编号bind事件
 * @param deviceControl
 */
function bindCombo_deviceId(deviceControl) {
    $.ajax({
        url: CONTEXT_PATH + "/dustProCheckPoint/getDeviceIdList.do",
        type: "get",
        success: function (data) {
            var deviceCombo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < data.length; i++) {
                deviceCombo.push({"text": data[i].deviceId, "value": data[i].deviceId});
            }
            $("#" + deviceControl).combobox("loadData", deviceCombo);
        }
    })
}

/**
 * 项目改变选项函数onChange事件
 * @param funControl
 */
function bindCombo_projectChange(deviceControl) {
    $("#prId_").combobox({
        onChange: function (n, o) {
            $("#deviceId_").combobox("clear"); //清空
            clear:true;
            $.ajax({
                url: CONTEXT_PATH + "/dustProCheckPoint/getDeviceIdList.do",
                data: {prId: n},
                type: "get",
                success: function (data) {
                    var deviceCombo = [{'text': '--请选择--', 'value': ''}];
                    for (var i = 0; i < data.length; i++) {
                        deviceCombo.push({"text": data[i].deviceId, "value": data[i].deviceId});
                    }
                    $("#" + deviceControl).combobox("loadData", deviceCombo);
                }
            })
        }
    });
}